package com.javaex.book.services.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javaex.book.mapper.BookMapper;
import com.javaex.book.pojo.Book;
import com.javaex.book.services.BookService;
import com.javaex.book.utils.ReturnResult;

@Service
public class BookServiceImpl implements BookService {
	
	@Autowired
	BookMapper bookMapper;

	@Override
	public Integer countBooks(BigDecimal minPrice, BigDecimal maxPrice) {
		return bookMapper.countBooks(minPrice, maxPrice);
	}

	@Override
	public Book getBookById(Integer id) {
		return bookMapper.selectBookById(id);
	}
	
	@Override
	public List<Book> getBookList(Integer start, Integer pageSize, BigDecimal minPrice, BigDecimal maxPrice) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("minPrice", minPrice);
		map.put("maxPrice", maxPrice);
		return bookMapper.selectBookList(map);
	}

	@Override
	public ReturnResult addBook(Book book) {
		Integer rows = bookMapper.insertBook(book);
		if (rows == null || rows ==0) {
			return ReturnResult.fail("图书添加失败");
		}
		return ReturnResult.success("图书添加成功");
	}

	@Override
	public ReturnResult updateBook(Book book) {
		Integer rows = bookMapper.updateBook(book);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("图书修改失败");
		}
		return ReturnResult.success("修改成功");
	}

	@Override
	public ReturnResult deleteBook(Integer id) {
		Integer rows = bookMapper.deleteBook(id);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("删除失败");
		}
		return ReturnResult.success("删除成功");
	}
}
